package com.orange.eduservice.controller;


import com.orange.commonutils.result.Result;
import com.orange.eduservice.client.VodClient;
import com.orange.eduservice.entity.EduVideo;
import com.orange.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author orange
 * @since 2021-12-17
 */
@RestController
@Api(tags = "小节管理")
@RequestMapping("/eduService/edu-video")
@CrossOrigin //开启跨域访问
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    //注入远程调用接口
    @Autowired
    private VodClient vodClient;

    //添加小节
    @ApiOperation("添加小节")
    @PostMapping("/addVideo")
    public Result addVideo(@ApiParam("小节对象") @RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return Result.ok();
    }

    //删除小节
    @ApiOperation("删除小节")
    @DeleteMapping("/deleteVideo/{id}")
    public Result deleteVideo(@ApiParam("小节id") @PathVariable String id) {
        //根据小节id获取视频id
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断视频id是否为空
        if(StringUtils.isNotEmpty(videoSourceId)){
            //根据视频id，实现远程调用删除云端视频
            vodClient.removeVideo(videoSourceId);
        }
        //删除小节
        eduVideoService.removeById(id);
        return Result.ok();
    }

    //修改小节
    @ApiOperation("修改小节")
    @PostMapping("/updateVideo")
    public Result updateVideo(@ApiParam("小节对象") @RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return Result.ok();
    }

    //根据小节id查询
    @ApiOperation("根据小节id查询")
    @GetMapping("/getVideoById/{videoId}")
    public Result getVideoById(@ApiParam("小节id") @PathVariable String videoId) {
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return Result.ok().data("video", eduVideo);
    }

}

